package com.morgankelly.action;

import java.util.ArrayList;
import java.util.List;

import com.morgankelly.po.Category;
import com.morgankelly.po.Tag;
import com.morgankelly.util.Constants;

@SuppressWarnings("serial")
public class CategoryAction extends BaseAction {

	public String input() {

		initCategoryForm();
		return INPUT;
	}
	
	public String add() {

	    Category category = new Category();
	    category.setName(name);
	    category.setDescription(description);
	    
	    getBlogService().addCategory(category);
	    
		initCategoryForm();
		return ADD;
	}
	
	public String updateInput() {
		
		Category category;
		
		List<Object> args = new ArrayList<Object>();
		if (categoryID == null || categoryID == 0) {
            args.add(Tag.class.getSimpleName());
            addActionError(getText(Constants.ERROR_ID_NOT_CHOSEN, args));
            return FAILED;
		} else {
			category = getBlogService().getCategoryByID(categoryID);
			setName(category.getName());
			setDescription(category.getDescription());
		}
		
		initCategoryForm();
		return UPDATE_INPUT;
	}
	
	public String update() {
		
		getBlogService().updateCategory(categoryID, name, description);
	    
		initCategoryForm();
		return UPDATE;
	}
	
	public String delete() {

		getBlogService().deleteCategoryByID(categoryID);
		
		initCategoryForm();
		return DELETE;
	}
	
	private void initCategoryForm() {
		setCategoryList(getBlogService().getCategoryList());
		initSideBar();
	}
	
	private String name;
    private String description;
    
    private Long categoryID;
    
	public Long getCategoryID() {
		return categoryID;
	}

	public void setCategoryID(Long categoryID) {
		this.categoryID = categoryID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
